package com.mlz.mlzmall.ui.activity.act.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.Gson;
import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.GetAwardPromotionResponse;
import com.mlz.mlzmall.ui.activity.act.base.BaseActivity;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.util.SharedUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * describe :
 * Created by Pm on 2016/5/31.
 * PackageName com.mlz.mlzmall.ui.activity.act.main.
 * ProjectName mlzmall_android.
 */

public class FreeTrialActivity extends BaseActivity{
    WebView wv_Free_trial;
    RelativeLayout back_from_trial;
    Button bottom_joined;
    String promotionIdUrl;
    Button bottom_joined_now;
    Button bottom_over;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_free_trial);
        initView();
        freeTrialView();

        initListener();
        initData();
    }

    private void initData() {
    }

    private void initListener() {

    }

    private void initView() {
        wv_Free_trial = (WebView) findViewById(R.id.wv_Free_trial);
        back_from_trial = (RelativeLayout)findViewById(R.id.back_from_trial);
        bottom_joined = (Button)findViewById(R.id.bottom_joined);//已参与活动
        bottom_joined_now = (Button)findViewById(R.id.bottom_joined_now);//马上参与
        bottom_over = (Button)findViewById(R.id.bottom_over);//活动已过期
        promotionIdUrl = getIntent().getStringExtra("promotionIdUrl");

    }
    private void freeTrialView( ) {
        try {
            wv_Free_trial.getSettings().setJavaScriptEnabled(true);
            wv_Free_trial.loadUrl(promotionIdUrl);
            wv_Free_trial.setWebViewClient(new WebViewClient());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getData()
    {
        JSONObject jsonObject = new JSONObject(getMall());
        JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.GET, "http://app.mlzmall.com/api/promotion/awardPromotion", jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                GetAwardPromotionResponse bean = gson.fromJson(jsonObject.toString(),GetAwardPromotionResponse.class);
                if(bean.getData()!=null)
                {
                    String status = bean.getData().getStatus();
                    if (status.equals("EXPIRED")){
                        bottom_over.setVisibility(View.VISIBLE);
                    }else if (status.equals("JOINED")){
                        bottom_joined.setVisibility(View.VISIBLE);

                        // 已经参加活动跳转
                        bottom_joined.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intentToJoin = new Intent(FreeTrialActivity.this, JoinActivity.class);
                                intentToJoin.putExtra("promotionIdUrl", promotionIdUrl);
                                startActivity(intentToJoin);
//                                            finish();
                                overridePendingTransition(R.anim.in_from_right_back_center, R.anim.out_to_left_back_center);
                            }
                        });
                    }else if (status.equals("UNJOINED")){
                        bottom_joined_now.setVisibility(View.VISIBLE);
                        bottom_joined_now.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
//                                Intent intent = new Intent();
//                                if(!MyApplication.getInstance().isLogin()){
//                                    intent.setClass(FreeTrialActivity.this, LoginActivity.class);
//                                }else
//                                    intent.setClass(FreeTrialActivity.this, AddressActivity.class);
//                                intent.putExtra("addressAdd",88);
//                                startActivity(intent);
//                                overridePendingTransition(R.anim.in_from_right_back_center, R.anim.out_to_left_back_center);
                            }
                        });

                    }
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("addCollection", error.getMessage(), error);

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put(ConfigInfo.CHARSET, ConfigInfo.UTF8);
                map.put(ConfigInfo.COOKIE, ConfigInfo.COOKIE_KEY + sessionId);
                map.put(ConfigInfo.TYPE, ConfigInfo.TYPE_KEY);
                return map;
            }
        };

        // 设置该请求的标签
        jsonRequest.setTag("addCollection");
        // 将请求添加到队列中
        ((MyApplication) getApplication()).getRequestQueue().add(jsonRequest);
    }

    private Map<String, String> getMall() {
        Map<String, String> param1 = new HashMap<String, String>();
        param1.put("promotionId", "" + SharedUtils.get(FreeTrialActivity.this,"promotionId",0));
        return param1;
    }
}
